package com.GameSpace.CoreAssets;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;
import android.util.Log;
import android.widget.LinearLayout;

public class Toolbar extends LinearLayout {
	private ArrayList<ToolBarItem> items;
	private int height;
	private int margin;
	private ToolBarType toolBarType;
	private boolean hasHomeButton;
	private boolean hasResetButton;
	private ToolbarHomeButton homeButton;
	private ToolbarResetButton resetButton;
	private LinearLayout.LayoutParams lp;
	
	
	//toolbar must have a Context argument in the constructor.
	public Toolbar(Context context,ToolBarType t) {
		super(context);
		height = 0;
		margin = 0;
		lp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,
			 height);
		this.setLayoutParams(lp);
		this.setOrientation(HORIZONTAL);
		this.setLayoutParams(lp);
		items = new ArrayList<ToolBarItem>();
		hasHomeButton = false;
		hasResetButton = false;
		toolBarType = t;
		// TODO Auto-generated constructor stub
	}

	public int addItem(ToolBarItem item){	//item at the end of the list
		ToolBarItem tmp = item;
		int i = items.size();
		if(hasResetButton){
			if (i==1)
				items.add(i-1,tmp);
			else
				items.add(i-2,tmp);
		}
		else{
			items.add(item);
		}
		return items.indexOf(tmp);   
	}
	
	public void updateLayout() {
		this.removeAllViewsInLayout();
		Log.d("updatelayout","size:"+items.size());
		Iterator<ToolBarItem> itr = items.iterator();
        /*
         * screen width is hardcoded to 320px
         */
		//int i = (320-(margin*(items.size()-1)))/items.size();
		int i = 320/items.size();
		Log.d("updatelayout","width "+i);
		int temp=0;
		//LayoutParams ll = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
		while(itr.hasNext()){
			ToolBarItem t = itr.next();
			this.addView(t);
			t.setWidth(i);
			Log.d("updatelayout","measured width "+super.getMeasuredWidth());
			temp++;
		}
		Log.d("temp","temp value"+String.valueOf(temp));
		Log.d("updatelayout","measured width "+super.getMeasuredWidth());
	}

	public boolean removeItem(int index){ //remove index
		if(index < 0 || index >= items.size())
	    return false;
		else{
		this.removeView(items.get(index));
		items.remove(index);
		}
		return true;
	}
	
	public void addHomeButton(ToolbarHomeButton thb){
		if(!hasHomeButton){
		homeButton = thb;
		items.add(0, thb);
		hasHomeButton = true;
		}
	}   
	
	public void addResetButton(ToolbarResetButton trb){
		if(!hasResetButton){
		resetButton = trb;
		items.add(resetButton);
		hasResetButton = true;
		Log.d("addresetbutton2",""+items.size());
		}
	}
	
	public void removeHomeButton(){
		homeButton = null;
		hasHomeButton = false;
		items.remove(0);
	}
	
	public void removeResetButton(){
		resetButton = null;
		hasResetButton = false;
		items.remove(items.size()-1);
	}
	
	public int getToolbarHeight(){
		return height;
	}
	
	public void setToolbarHeight(int h){
		height = h;
		lp = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,
				 height);
	}
	
	public int getToolbarMargin(){
		return margin;
	}
	
	public void setToolbarMargin(int m){
		margin = m;
	}	
	
	public ToolbarHomeButton getHomeButton(){
		return homeButton;
	}
	
	public ToolbarResetButton getResetButton(){
		return resetButton;
	}
	
	public ToolBarItem getToolbarItem(int index){
		return items.get(index);
	}
	
	public ArrayList<ToolBarItem> getToolbarItem(){
		return items;
	}
	
	public boolean hasHomeButton(){
		return hasHomeButton;
	}
	
	public boolean hasResetButton(){
		return hasResetButton;
	}
	
	public ToolBarType getToolbarType(){
		return toolBarType;
	}
	
	public void setToolBarType(ToolBarType t){
		toolBarType = t;
	}
}
